package com.briup.estore.adminAPI.web.controller;

import com.briup.estore.common.service.ICategoryService;
import com.briup.estore.common.service.IOrderService;
import com.briup.estore.common.service.IShopService;
import com.briup.estore.common.service.IUserService;
import com.briup.estore.common.util.DateUtil;
import com.briup.estore.common.util.RedisCache;
import com.briup.estore.common.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author adam
 * @date 2023/5/10
 */
@RestController
@CrossOrigin
@RequestMapping("/index")
@Api(tags = "基本数据管理")
@Slf4j
public class IndexController {
    @Autowired
    private  RedisCache redisCache;
    @Autowired
    private  IUserService userService;
    @Autowired
    private IShopService shopService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private  IOrderService orderService;


    @GetMapping("dashboard")
    @ApiOperation("首页仪表盘数据")
    public Result dashboard() {
        HashMap<String, Long> map = new HashMap<>();
        long userNum = userService.countUser();
        long shopNum = shopService.countShop();
        long categoryNum = categoryService.countTwoLevelCategory();
        long orderNum = orderService.countOrder();
        map.put("userNum", userNum);
        map.put("shopNum", shopNum);
        map.put("categoryNum", categoryNum);
        map.put("orderNum", orderNum);
        log.info(map.toString());
        return Result.success(map);
    }

    @GetMapping("sexRatio")
    @ApiOperation("用户性别比例图")
    public Result sexRatio() {
        Map<String, Double> map = userService.sexRatio();

        return Result.success(map);
    }

    @GetMapping("categoryStats")
    @ApiOperation("商品分类统计")
    public Result categoryStats() {
        Map<String, Long> map = categoryService.categoryStats();
        return Result.success(map);
    }

    @GetMapping("countOnLine")
    @ApiOperation("网站流量数据统计")
    public Result countOnLine() {
        Integer pv = redisCache.getCacheObject(DateUtil.getNowDate() + ":pv");
        Integer online = redisCache.getCacheObject("online");
        Integer visitors = redisCache.getCacheObject(":visitors");
        HashMap<String, Integer> map = new HashMap<>();
        map.put("pv", Objects.isNull(pv)?0:pv);
        map.put("online", Objects.isNull(online)?0:online);
        map.put("visitors", Objects.isNull(visitors)?0:visitors);
        return Result.success(map);
    }
}

